package com.so.np2.algs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
	static String LINE = "-";
	static String SPACE = " ";

	static List<Request> readRequests(File f) throws NumberFormatException {

		ArrayList<Request> toReturn = new ArrayList<Request>();

		try {
			BufferedReader in = new BufferedReader(new FileReader(f));
			String line;
			while ((line = in.readLine()) != null) {
				String[] requests = line.split(LINE);
				if (requests == null || requests.length == 1) {
					requests = line.split(SPACE);
				}
				for (int i = 0; i < requests.length; i++) {
					String op = requests[i];
					char operacao = op.charAt(op.length() - 1);
					op = op.substring(0, op.length() - 1);
					int pagina = Integer.parseInt(String.valueOf(op));
					toReturn.add(new Request(pagina, operacao));
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return toReturn;
	}

}